package lsl.leetCode.year2023.test0203;

import java.util.Arrays;

import static java.util.Arrays.sort;

/**
 * description:
 *
 * @author: liushengli
 * data: 2023/2/3
 */
//977. 有序数组的平方
public class test977 {
    public static void main(String[] args) {
        int[] nums = {-4, -1, 0, 3, 10};
        sortedSquares(nums);
    }

    //双指针
    public static int[] sortedSquares(int[] nums) {
        int[] resultPos = new int[nums.length];
        for (int i = 0, j = nums.length - 1, pre = nums.length - 1; i <= j; ) {
            if (nums[i] * nums[i] < nums[j] * nums[j]) {
                resultPos[pre] = nums[j] * nums[j];
                j--;
            } else {
                resultPos[pre] = nums[i] * nums[i];
                i++;
            }
            pre--;
        }
        return resultPos;
    }
}
